package cn.pigeon.health.service.impl;

import cn.pigeon.health.dao.MemberDao;
import cn.pigeon.health.dao.OrderDao;
import cn.pigeon.health.dao.ReportDao;
import cn.pigeon.health.service.ReportService;
import cn.pigeon.health.utils.DateUtils_Extend;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ReportDao reportDao;

    /**
     * 查询运营数据
     * @return
     */
    public Map<String, Object> getBusinessReportData() throws Exception {

        //当前日期
        Date today = new Date();

        //当前日期（格式化后）
        String reportDate = DateUtils_Extend.parseDate2String(today);
        //本星期一所对应的日期
        String thisWeekMonday = DateUtils_Extend.parseDate2String(DateUtils_Extend.getThisWeekMonday(today));
        //本月第一天对应的日期
        String thisMonthFirstDay = DateUtils_Extend.parseDate2String(DateUtils_Extend.getFirstDay4ThisMonth());

        //当天新增会员人数
        Integer todayNewMember = memberDao.findCountByDate(reportDate);
        //总会员人数
        Integer totalMember = memberDao.getTotalCount();
        //本星期新增会员人数
        Integer thisWeekNewMember = memberDao.findCountByAfterDate(thisWeekMonday);
        //本月新增会员人数
        Integer thisMonthNewMember = memberDao.findCountByAfterDate(thisMonthFirstDay);

        //本日预约人数
        Integer todayOrderNumber = orderDao.findCountByDate(reportDate);
        //本日到诊人数
        Integer todayVisitsNumber = orderDao.findCountByDate_DZ(reportDate);
        //本周预约人数
        Integer thisWeekOrderNumber = orderDao.findCountByAfterDate(thisWeekMonday);
        //本周到诊人数
        Integer thisWeekVisitsNumber = orderDao.findCountByAfterDate_DZ(thisWeekMonday);
        //本月预约人数
        Integer thisMonthOrderNumber = orderDao.findCountByAfterDate(thisMonthFirstDay);
        //本月到诊人数
        Integer thisMonthVisitsNumber = orderDao.findCountByAfterDate_DZ(thisMonthFirstDay);

        //热门套餐
        List<Map<String,Object>> hotSetmeal = reportDao.findHotSetmeals();

        Map<String,Object> mapReturn = new HashMap<>();
        mapReturn.put("reportDate",reportDate);
        mapReturn.put("todayNewMember",todayNewMember);
        mapReturn.put("totalMember",totalMember);
        mapReturn.put("thisWeekNewMember",thisWeekNewMember);
        mapReturn.put("thisMonthNewMember",thisMonthNewMember);
        mapReturn.put("todayOrderNumber",todayOrderNumber);
        mapReturn.put("todayVisitsNumber",todayVisitsNumber);
        mapReturn.put("thisWeekOrderNumber",thisWeekOrderNumber);
        mapReturn.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        mapReturn.put("thisMonthOrderNumber",thisMonthOrderNumber);
        mapReturn.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        mapReturn.put("hotSetmeal",hotSetmeal);

        return mapReturn;
    }
}
